#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from model import CourseM
from model import InviteM

class inviteHandler(webapp.RequestHandler):
    def post(self):
        gmail = self.request.get("gmail")
        courseKey = self.request.get("courseKey")
        if courseKey:
            course = CourseM.getCourseByKey(courseKey)
        if gmail and course:
            result = InviteM.addInvite(gmail, course)
            if isinstance(result, basestring):
                self.response.out.write(result)
            elif isinstance(result, InviteM.Ownership):
                self.redirect("/course?key=" + str(courseKey))
                #InviteM.sendInvite(gmail, name, course.name, result.key())
                #(googleID, fullName, courseName, ownershipKey):
        else:
            pass

application = webapp.WSGIApplication([(r'.*', inviteHandler)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()